;**********************************************;
;* Project Title: video screen drawing demo   *;
;* Version: 29092005                          *;
;**********************************************;
assume  ss:stk, cs:code, ds:data

;==========================================================================
;                               stack segment
;=========================================================================={{{
stk     segment stack
	db      256 dup (?)             ; allocate 256 bytes for stack
tos     label   word                    ; alignment by word
stk     ends
;}}}

;==========================================================================
;                               data segment
;=========================================================================={{{
data    segment

;---------------------------------------------------------------------          
; Color constants
;---------------------------------------------------------------------          
	black           equ     0000b           ; 00h
	blue            equ     0001b           ; 01h
	green           equ     0010b           ; 02h
	cyan            equ     0011b           ; 03h
	red             equ     0100b           ; 04h
	magenta         equ     0101b           ; 05h
	brown           equ     0110b           ; 06h
	lightgray       equ     0111b           ; 07h
	darkgray        equ     1000b           ; 08h
	lightblue       equ     1001b           ; 09h
	lightgreen      equ     1010b           ; 0ah
	lightcyan       equ     1011b           ; 0bh
	lightred        equ     1100b           ; 0ch
	lightmagenta    equ     1101b           ; 0dh
	yellow          equ     1110b           ; 0eh
	white           equ     1111b           ; 0fh

;---------------------------------------------------------------------         
; Message variables
;---------------------------------------------------------------------          
        wel_msg         db      'Welcome to the j-monopoly-asm', '$'
	key_msg         db      'Press any key to continue', '$'
        wof_msg         db      'Monopoly v0.1', '$'

	vscreen		db	307200	dup (?)

data    ends
;}}}



;==========================================================================
;                                Macros
;==========================================================================

;--------------------------------------------------------------------------
; draws box
;--------------------------------------------------------------------------{{{
box     macro   row_start, row_end, col_start, col_end, color
	local   next_row, next_col, done

        push    ax                      ; saves previous contents of ax into stack
        push    dx                      ; saves previous contents of dx into stack
        push    cx                      ; saves previous contents of cx into stack

	mov     dx, row_start           ; initialise dx with start of row

next_row:
	mov     cx, col_start           ; initialise cx with start of col

next_col:
	mov     ah, 0ch                 ; write colored pixel to screen
	mov     al, color
	int     10h

	inc     cx                      ; increment cx by 1
	cmp     cx, col_end             ; check to see if cx has reached col end
	jne     next_col                ; if not equal, go to next col
					
	inc     dx                      ; else, increment dx by 1
	cmp     dx, row_end             ; check to see if dx has reached row end
	jne     next_row                ; if not equal, go to next row

        pop     cx                      ; retrieves contents into cx from the stack
        pop     dx                      ; retrieves contents into dx from the stack
        pop     ax                      ; retrieves contents into ax from the stack

endm
;}}}

;--------------------------------------------------------------------------
; draws horizontal lines
;--------------------------------------------------------------------------{{{
hline   macro   row, col_start, col_end, color
	local   next_col

        push    ax                      ; saves previous contents of ax into stack
        push    dx                      ; saves previous contents of dx into stack
        push    cx                      ; saves previous contents of cx into stack

	mov     dx, row                 ; initialise dx with row
	mov     cx, col_start           ; initialise cx with start of col
	
next_col:
	mov     ah, 0ch                 ; write colored pixel to screen
	mov     al, color
	int     10h

	inc     cx                      ; increment cx by 1
	cmp     cx, col_end             ; check to see if cx has reached col end
	jne     next_col                ; if not equal, go to next col

        pop     cx                      ; retrieves contents into cx from the stack
        pop     dx                      ; retrieves contents into dx from the stack
        pop     ax                      ; retrieves contents into ax from the stack

endm
;}}}

;--------------------------------------------------------------------------
; draws vertical lines
;--------------------------------------------------------------------------{{{
vline   macro   row_start, row_end, col, color
	local   next_row

        push    ax                      ; saves previous contents of ax into stack
        push    dx                      ; saves previous contents of dx into stack
        push    cx                      ; saves previous contents of cx into stack

	mov     cx, col                 ; initialise cx with col
	mov     dx, row_start           ; initialise dx with start of row

next_row:
	mov     ah, 0ch                 ; write colored pixel to screen
	mov     al, color
	int     10h

	inc     dx                      ; increment dx by 1
	cmp     dx, row_end             ; check to see if dx has reached row end
	jne     next_row                ; if not equal, go to next row
						   
        pop     cx                      ; retrieves contents into cx from the stack
        pop     dx                      ; retrieves contents into dx from the stack
        pop     ax                      ; retrieves contents into ax from the stack

endm
;}}}

;--------------------------------------------------------------------------
; changes color
;--------------------------------------------------------------------------{{{
change_color    macro   row_start, row_end, col_start, col_end, color_old, color_new
	local   check_color, next_row, next_col, new_color, continue, complete

	push    ax                      ; stores contents of ax, bx, cx and dx into the stack
	push    bx
	push    cx
	push    dx

check_color:
	mov     dx, row_start           ; initialise dx with start of row

next_row:
	mov     cx, col_start           ; initialise cx with start of col

next_col:
	mov     ah, 0dh                 ; retrieves color of pixel from opponent's grid
	mov     bh, 00h
	int     10h

	cmp     al, color_old
	je      new_color
	jmp     continue

new_color:
	mov     ah, 0ch                 ; write colored pixel to screen
	mov     al, color_new
	int     10h

continue:
	inc     cx                      ; increment cx by 1
	cmp     cx, col_end             ; check to see if cx has reached col end
	jne     next_col                ; if not equal, go to next col
				       
	inc     dx                      ; else, increment dx by 1
	cmp     dx, row_end             ; check to see if dx has reached row end
	jne     next_row                ; if not equal, go to next row

complete:
	pop     dx                      ; retrieves contents into ax, bx, cx and dx from the stack
	pop     cx
	pop     bx
	pop     ax
	
endm
;}}}


;--------------------------------------------------------------------------
; position keyboard cursor to coordinates (row, column)
;--------------------------------------------------------------------------{{{
cursor  macro   row, column

	push    ax                      ; saves previous contents of ax into stack
        push    bx                      ; saves previous contents of bx into stack
        push    dx                      ; saves previous contents of dx into stack

        mov     ah, 02h                  ; set cursor at position (row, column)
	mov     bh, 00h

	mov     dh, row
	mov     dl, column
	int     10h

        pop     dx                      ; retrieves previous contents of dx from stack
        pop     bx                      ; retrieves previous contents of bx from stack
        pop     ax                      ; retrieves previous contents of ax from stack

endm
;}}}

;--------------------------------------------------------------------------
; displays message
;--------------------------------------------------------------------------{{{
message macro   msg

	push    ax                      ; saves previous contents of ax into stack
        push    dx                      ; saves previous contents of dx into stack

	lea     dx, msg                 ; displays message
	mov     ah, 09h
	int     21h

        pop     dx                      ; retrieves previous contents of dx from stack
        pop     ax                      ; retrieves previous contents of ax from stack

endm
;}}}




;==========================================================================
;                               code segment
;==========================================================================
code    segment
start:
	mov     ax, stk                 ; initialise stack segment
	mov     ss, ax
	mov     ax, offset tos
	mov     sp, ax
	mov     ax, data                ; initialise data segment
	mov     ds, ax

	mov	ah, 0fh			; get current video mode
	int	10h
	push	ax

set_screen:
	mov     ah, 00h                 ; set resolution to 640 x 480
	mov     al, 12h                 ; 16 colors
	int     10h

clear_registers:
	call    clear_reg               ; clears ax, bx, cx and dx registers

welcome_message:
        call    welcome                 ; welcome message

draw_interface:
	call    interface               ; displays interface


;---------------------------------------------------------------------
wait_for_exit:
	mov     ah, 0bh
	int     21h

	cmp     al, 0ffh
	jne     wait_for_exit
;---------------------------------------------------------------------

exit:
	pop	ax
	mov     ah, 00h                 ; set back to original video mode
	int     10h

        mov     ah, 0ch                 ; clear keyboard buffer
        int     21h

	mov     ah, 4ch                 ; exit program
	int     21h
;--------------------------------------------------------------------------




;==========================================================================
;                               Procedures
;==========================================================================

;--------------------------------------------------------------------------
; clears registers
;--------------------------------------------------------------------------{{{
clear_reg       proc    near

	xor     ax, ax
	xor     bx, bx
	xor     cx, cx
	xor     dx, dx

	ret

clear_reg       endp
;}}}

;--------------------------------------------------------------------------
; welcome message
;--------------------------------------------------------------------------{{{
welcome proc    near

        push    ax                      ; saves previous contents of ax into stack

        cursor  12, 25                  ; display "Welcome to the Word of Fortune Game"
	message wel_msg
        cursor  14, 29                  ; display "Press any key to continue"
	message key_msg

wait_for_key:
	mov     ah, 0bh                 ; wait for any key to be pressed
	int     21h

	cmp     al, 0ffh
	jne     wait_for_key

done_wel:
        box     150, 300, 200, 500, black       ; clear message area

        mov     ah, 0ch                 ; clear keyboard buffer
        int     21h

	pop     ax                      ; retrieves contents into ax from the stack
	
	ret

welcome endp
;}}}

;--------------------------------------------------------------------------
; draws interface
;--------------------------------------------------------------------------{{{
interface       proc    near

        call    game_title              ; display title of game
        call    squares                 ; display squares where the alphabets sit
	mov	ax, 01h
	int	33h
	ret

interface       endp
;}}}

;--------------------------------------------------------------------------
; displays title
;--------------------------------------------------------------------------{{{
game_title      proc    near

        cursor  00, 32                          ; displays "Name of the Game"
        message wof_msg

        change_color    00, 50, 200, 400, lightgray, lightcyan   ; changes color of titles

	ret

game_title      endp
;}}}

;--------------------------------------------------------------------------
; alignment for squares
;--------------------------------------------------------------------------{{{
squares proc    near

	; begin game grid
        hline   25, 1, 220, white
        hline   25, 293, 512, white

	hline	111, 1, 512, white

	hline	197, 1, 74, white
	hline	197, 147, 366, white
	hline	197, 439, 512, white

	hline	283, 1, 74, white
	hline	283, 147, 366, white
	hline	283, 439, 512, white

	hline	369, 1, 512, white

	hline	455, 1, 220, white
	hline	455, 293, 512, white

	vline	25, 455, 1, white
	vline	25, 455, 74, white

	vline	25, 197, 147, white
	vline	283, 455, 147, white
	vline	25, 197, 220, white
	vline	283, 455, 220, white
	vline	25, 197, 293, white
	vline	283, 455, 293, white
	vline	25, 197, 366, white
	vline	283, 455, 366, white

	vline	25, 455, 439, white
	vline	25, 455, 512, white

	; begin game heading color
	box	370, 393, 367, 439, lightblue
	box	370, 393, 294, 366, lightblue
	box	284, 307, 294, 366, lightblue
	box	284, 307, 148, 220, magenta
	box	370, 393, 148, 220, magenta
	box	370, 393, 75, 147, magenta
	box	284, 307, 2, 74, red
	box	198, 220, 2, 74, red
	box	112, 135, 2, 74, red
	box	26, 49, 75, 147, brown
	box	26, 49, 148, 220, brown
	box	112, 135, 148, 220, brown
	box	112, 135, 294, 366, green
	box	26, 49, 294, 366, green
	box	26, 49, 367, 439, green
	box	112, 135, 440, 512, blue
	box	198, 220, 440, 512, blue
	box	284, 307, 440, 512, blue

	; begin action window
	hline	29, 518, 639, yellow
	hline	451, 518, 639, yellow
	vline	29, 451, 518, yellow
	vline	29, 451, 639, yellow

        ret

squares endp
;}}}

;--------------------------------------------------------------------------
; clears the screen (FAST!)
;--------------------------------------------------------------------------{{{
clrscrn	proc
	
ENDP	clrscrn



;==========================================================================
code    ends
end     start
;==========================================================================
